package com.platform.common.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * <pre>
 * 功能：提供HTTP方面的通用类
 * </pre>
 * 
 * @author wei.gao
 */
public class HttpUtils {
	/**
	 * <pre>
	 * 功能：通过post
	 * </pre>
	 * 
	 * @param urlStr
	 * @param content
	 * @param charsetName
	 * @return
	 * @throws IOException
	 */
	public static String getPostResult(String urlStr, String content, String charsetName) throws IOException {
		URL url = null;
		HttpURLConnection connection = null;
		try {
			url = new URL(urlStr);
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.connect();
			
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			out.writeBytes(content);
			out.flush();
			out.close();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
			StringBuffer buffer = new StringBuffer();
			String line = "";
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}
			reader.close();
			return buffer.toString();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
	}
	
	/**
	 * <pre>
	 * 功能：通过GET方法获得
	 * </pre>
	 * 
	 * @param urlStr
	 * @param charsetName
	 * @return
	 * @throws IOException
	 */
	public static String getGetResult(String urlStr, String charsetName) throws IOException {
		URL url = null;
		HttpURLConnection connection = null;
		try {
			url = new URL(urlStr);
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("GET");
			connection.setUseCaches(false);
			connection.connect();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), charsetName));
			StringBuffer buffer = new StringBuffer();
			String line = "";
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}
			reader.close();
			return buffer.toString();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
	}
	
	/**
	 * <pre>
	 * 功能：地址转码
	 * </pre>
	 * 
	 * @param str
	 * @param charset
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getURLEncode(String str, String charset) throws UnsupportedEncodingException {
		if (ValidateUtils.isNull(str)) {
			return null;
		}
		return URLEncoder.encode(str, ValidateUtils.isNull(charset) ? Configuration.getEncoding() : charset);
	}
	
	/**
	 * <pre>
	 * 功能：地址转码
	 * </pre>
	 * 
	 * @param str
	 * @param charset
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getURLDecode(String str, String charset) throws UnsupportedEncodingException {
		if (ValidateUtils.isNull(str)) {
			return null;
		}
		String result = URLDecoder.decode(str, ValidateUtils.isNull(charset) ? Configuration.getEncoding() : charset);
		return result;
	}
}
